package gov.pbc.nn.kjgl.software.view;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.kjgl.entity.Project;
import gov.pbc.nn.kjgl.software.dao.HldShDao;
import gov.pbc.nn.uflo.entity.Opinion;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;
import com.bstek.dorado.annotation.Expose;
import com.bstek.uflo.client.service.ProcessClient;
import com.bstek.uflo.client.service.TaskClient;
import com.bstek.uflo.model.ProcessInstance;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.service.TaskOpinion;

/**
 * 行领导审核的pr类，主要写各个行领导的调用的业务逻辑
 * 
 * @author jack.zhao
 * 
 */
@Component("hldShPR")
public class HldShPR {

	@Autowired
	private HldShDao hldShDao;
	@Autowired
	@Qualifier(ProcessClient.BEAN_ID)
	private ProcessClient processClient;

	@Autowired
	@Qualifier(TaskClient.BEAN_ID)
	private TaskClient taskClient;

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao coreHibernateDao;

	/**
	 * 传入taskID，和opinion，并且开始和完成任务 行领导同意的页面 同意是8，不同意是9
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxOk(String taskId, String opinion) {
		// 3
		TaskOpinion option = new TaskOpinion(opinion);

		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 8);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("集中式立项申请");
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setNodeName(task.getNodeName());
		// 保存意见
		this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Serializable>() {
			@Override
			public Serializable doInSession(Session session) {
				o.setCreateUser(ContextHolder.getLoginUserName());
				o.setCreateDate(new Date());
				return session.save(o);
			}
		});
		// this.hldShDao.save(o);
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), map, option);
	}

	@Expose
	@Transactional
	public void ajaxOks(String taskId, String opinion, String dm) {
		// 3
		TaskOpinion option = new TaskOpinion(opinion);

		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 8);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		if (dm.equals("1")) {
			o.setProcessName("集中式立项申请");
		} else if (dm.equals("2")) {
			o.setProcessName("非集中式立项申请");
		}
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		// 保存意见
		this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Serializable>() {
			@Override
			public Serializable doInSession(Session session) {
				o.setCreateUser(ContextHolder.getLoginUserName());
				o.setCreateDate(new Date());
				return session.save(o);
			}
		});
		// this.hldShDao.save(o);
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), map, option);
	}

	/**
	 * 不通过流程和通过流程的处理逻辑类似
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxNo(String taskId, String opinion) {
		// 4
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程

		Map<String, Object> map = new HashMap<String, Object>();

		taskClient.start(Long.valueOf(taskId));

		map.put("state", 9);

		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("集中式立项申请");

		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setNodeName(task.getNodeName());
		// 保存意见
		// this.hldShDao.save(o);
		this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Serializable>() {
			@Override
			public Serializable doInSession(Session session) {
				o.setCreateUser(ContextHolder.getLoginUserName());
				o.setCreateDate(new Date());
				return session.save(o);
			}
		});
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), map, option);

	}

	@Expose
	@Transactional
	public void ajaxNos(String taskId, String opinion, String dm) {
		// 4
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程

		Map<String, Object> map = new HashMap<String, Object>();

		taskClient.start(Long.valueOf(taskId));

		map.put("state", 9);

		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		if (dm.equals("1")) {
			o.setProcessName("集中式立项申请");
		} else if (dm.equals("2")) {
			o.setProcessName("非集中式立项申请");
		}
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setNodeName(task.getNodeName());
		// 保存意见
		// this.hldShDao.save(o);
		this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Serializable>() {
			@Override
			public Serializable doInSession(Session session) {
				o.setCreateUser(ContextHolder.getLoginUserName());
				o.setCreateDate(new Date());
				return session.save(o);
			}
		});
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), map, option);

	}

	/**
	 * ┏┛┻━━━┛┻┓ ┃｜｜｜｜｜｜｜┃ ┃ ━ ┃ ┃ ┳┛ ┗┳ ┃ 盖楼是一种态度 ┃ ┃ ┃ ┻ ┃ ┃ ┃ 盖楼是为了提高知名度 ┗━┓
	 * ┏━┛ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┃ ┗━━━┓ ┃楼楼与我同在 ┣┓ ┃盖楼专用宠物 ┃ ┗┓┓┏━┳┓┏┛ ┃┫┫ ┃┫┫ ┗┻┛
	 * ┗┻┛
	 */
	/**
	 * 传入taskID，和opinion，并且开始和完成任务 科技行领导同意的页面 同意是5,不同意6
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxZgOk(String taskId, String opinion, String declareMeans) {
		TaskOpinion option = new TaskOpinion(opinion);

		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		if ("1".equals(declareMeans)) {
			o.setProcessName("集中式立项申请");
		} else {
			o.setProcessName("非集中式立项申请");
		}
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		o.setNodeName(task.getNodeName());
		// 保存意见
		// this.hldShDao.save(o);
		this.coreHibernateDao.save(o);
		// 完成流程
		//判断 如果是集中式，则同意的时候 直接同意给科技科。如果是非集中式，判断流程变量
		if ("1".equals(declareMeans)) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("state", 5);
			taskClient.complete(Long.valueOf(taskId), "to 科技科审核",map, option);
		} else {
			// 判断流程变量，如果state=5 则是业务行领导审核通过的，同意返回给科技处。如果state =2 则是科技科初审的。同意给主管业务行领导
			long processInstanceId = task.getProcessInstanceId();
			ProcessInstance process = processClient.getProcessInstanceById(Long.valueOf(processInstanceId));
			String state = processClient.getProcessVariable("state", process).toString();
			if (state.equals("5")) {
				taskClient.complete(Long.valueOf(taskId), "to 科技科审核", option);
			} else {
				taskClient.complete(Long.valueOf(taskId), "to 主管业务行领导审核", option);
			}
		}
	}

	@Expose
	@Transactional
	public void ajaxZgNo(String taskId, String opinion, String declareMeans) {
		// 4
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		taskClient.start(Long.valueOf(taskId));
		map.put("state", 6);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setNodeName(task.getNodeName());
		if ("1".equals(declareMeans)) {
			o.setProcessName("集中式立项申请");
		} else {
			o.setProcessName("非集中式立项申请");
		}

		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		// 保存意见
		// this.hldShDao.save(o);
		this.coreHibernateDao.save(o);
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), "to 科技科审核", map, option);
	}

	/**
	 * 皮卡丘分割线
	 * 
	 * 
	 * へ ／| /＼7 ∠＿/ / │ ／ ／ │ Z ＿,＜ ／ /`ヽ │ ヽ / 〉 Y ` / / ｲ● ､ ● ⊂⊃〈 / () へ | ＼〈
	 * >ｰ ､_ ィ │ ／／ / へ / ﾉ＜| ＼＼ ヽ_ﾉ (_／ │／／ 7 |／ ＞―r￣￣`ｰ―＿
	 */
	// 非集中式立项申报
	/**
	 * 到开始的state是15
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxZgBack(String taskId, String opinion) {
		// 7,设置为回退的值
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		// 设置状态
		map.put("state", 15);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("非集中式立项申请");
		o.setNodeName(task.getNodeName());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		// this.hldShDao.save(o);
		this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Serializable>() {
			@Override
			public Serializable doInSession(Session session) {
				o.setCreateUser(ContextHolder.getLoginUserName());
				o.setCreateDate(new Date());
				return session.save(o);
			}
		});
		taskClient.complete(Long.valueOf(taskId), "退回", map, option);
	}

	/**
	 * 不通过流程和通过流程的处理逻辑类似
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxZgOver(String taskId, String opinion) {
		// 4
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		Map<String, Object> map = new HashMap<String, Object>();
		taskClient.start(Long.valueOf(taskId));
		// 设置状态
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		o.setProcessName("非集中式立项申请");
		o.setNodeName(task.getNodeName());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		// 保存意见

		// this.hldShDao.save(o);
		this.coreHibernateDao.doInHibernateSession(new ISessionCallback<Serializable>() {
			@Override
			public Serializable doInSession(Session session) {
				o.setCreateUser(ContextHolder.getLoginUserName());
				o.setCreateDate(new Date());
				return session.save(o);
			}
		});
		// 更新或者删除申请表
		String busnessId = task.getBusinessId();
		Project project = this.hldShDao.getProjectById(busnessId);
		project.setState(10);
		this.hldShDao.update(project);
		// this.hldShDao.delete(busnessId, Project.class);
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), "to 结束2", map, option);
	}

	/**
	 * 后来加入的审核页面的方法 返回的状态和科技行领导一样。是为了方便以前页面的维护不用修改state。
	 * 
	 * 传入taskID，和opinion，并且开始和完成任务 业务行领导同意的页面 同意是5,不同意6
	 * 
	 * @param taskId
	 * @param opinion
	 */
	@Expose
	@Transactional
	public void ajaxZgywOk(String taskId, String opinion, String declareMeans) {
		TaskOpinion option = new TaskOpinion(opinion);

		Task task = taskClient.getTask(Long.valueOf(taskId));
		// 开始流程
		taskClient.start(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("state", 5);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		if ("1".equals(declareMeans)) {
			o.setProcessName("集中式立项申请");
		} else {
			o.setProcessName("非集中式立项申请");
		}
		o.setNodeName(task.getNodeName());
		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		// 保存意见
		// this.hldShDao.save(o);
		this.coreHibernateDao.save(o);
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), map, option);
	}

	@Expose
	@Transactional
	public void ajaxZgywNo(String taskId, String opinion, String declareMeans) {
		// 4
		TaskOpinion option = new TaskOpinion(opinion);
		Task task = taskClient.getTask(Long.valueOf(taskId));
		Map<String, Object> map = new HashMap<String, Object>();
		taskClient.start(Long.valueOf(taskId));
		map.put("state", 6);
		final Opinion o = new Opinion();
		o.setBusKey(task.getBusinessId());
		o.setTaskId(Long.parseLong(taskId));
		o.setNodeName(task.getNodeName());
		// 操作那张表呢
		o.setBusTable("T_PROJECT");
		o.setOpinion(opinion);
		o.setTaskName(task.getTaskName());
		if ("1".equals(declareMeans)) {
			o.setProcessName("集中式立项申请");
		} else {
			o.setProcessName("非集中式立项申请");
		}

		o.setDeptId(ContextHolder.getLoginUser().getDepts().get(0).getId());
		o.setDeptName(ContextHolder.getLoginUser().getDepts().get(0).getName());
		o.setUsername(ContextHolder.getLoginUserName());
		o.setCname(ContextHolder.getLoginUser().getCname());
		// 保存意见
		// this.hldShDao.save(o);
		this.coreHibernateDao.save(o);
		// 完成流程
		taskClient.complete(Long.valueOf(taskId), map, option);
	}

}
